package com.tianchi.lexiang.background;

import java.io.File;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.bean.LeXiangConstants;
import com.tianchi.lexiang.util.FtpUnit;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StatFs;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class NetworkAddSong extends Activity implements OnItemClickListener {
	
	public static final String NETWORK_ADD_SONG = "NetworkAddSong";

	private ListView backgroundSettingList;
	private Button backBtn;
	
	private ProgressDialog mProgressDialog;
	
	private FtpUnit ftpUnit;
	
	private long residualSize;
	private long canAddSongNum;
	
	private static final String remoteFileName = LeXiangConstants.INTERNET_DATABASE_PATH;
	private static final String localFileName = LeXiangConstants.DOWN_LOAD_PATH;
	
	public static final String RESIDUAL_SIZE = "RESIDUAL_SIZE";
	public static final String CAN_ADD_SONG_NUM = "CAN_ADD_SONG_NUM";
	
	private LanAddSongIPSetting lasIp;

	@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.background_setting);
			initView();
			lasIp = LanAddSongIPSetting.getInstance();
		}

	private void initView() {
		backgroundSettingList = (ListView) findViewById(R.id.background_setting_list);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.getResources().getStringArray(R.array.network_add_song));
		backgroundSettingList.setAdapter(adapter);
		backgroundSettingList.setOnItemClickListener(this);
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
				NetworkAddSong.this.finish();
				}
				});
	}

	@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
				case 0:
					lasIp.openDialog(NetworkAddSong.this, NETWORK_ADD_SONG);
			//		intent.setClass(NetworkAddSong.this, LanAddSong.class)
					break;
				case 1:
					if(ftpUnit == null){
						ftpUnit = FtpUnit.getInstance();
					}
					mProgressDialog = ProgressDialog.show(NetworkAddSong.this, getString(R.string.are_download), getString(R.string.download_in), true);
					new InterAddSongTask().execute();
					break;
				default:
					break;
			}
		}
	
	class InterAddSongTask extends AsyncTask<String, String, Boolean> {
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... params) {
			if (ftpUnit.loadFile(remoteFileName, localFileName)) {
				canAddNumberSong();
				return true;
			}
			return false;
		}
		
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if(result){
				mProgressDialog.dismiss();
				Toast.makeText(NetworkAddSong.this,getString(R.string.download_success), Toast.LENGTH_SHORT).show();
				Intent intent = new Intent();
				intent.putExtra(RESIDUAL_SIZE, residualSize);
				intent.putExtra(CAN_ADD_SONG_NUM, canAddSongNum);
				intent.setClass(NetworkAddSong.this, InternetAddSong.class);
				NetworkAddSong.this.startActivity(intent);
			}else{
				mProgressDialog.dismiss();
				Toast.makeText(NetworkAddSong.this,getString(R.string.download_failure), Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	private void canAddNumberSong(){
		File file = new File(LeXiangConstants.SONG_HARD_DISK_PATH);
		if(file.exists()){
			StatFs sf = new StatFs(LeXiangConstants.SONG_HARD_DISK_PATH);
			long blockSize = sf.getBlockSize();
			long availCount = sf.getAvailableBlocks();
			residualSize = availCount * blockSize / 1024 / 1024 / 1024;
			canAddSongNum = (residualSize * 1024) / 60;
		}
	}
}
